var N2S = 1000000000.0;

function Calculate(){

    this.v0x = 0.0;
    this.vtx = 0.0;
    this.lax = 0.0;

    this.v0y = 0.0;
    this.vty = 0.0;
    this.lay = 0.0;

    this.v0z = 0.0;
    this.vtz = 0.0;
    this.laz = 0.0;
    
    this.t = null;

    this.distance = function(xLAcc, yLAcc, zLAcc, time){
    // this.distance = function(xLAcc, yLAcc, zLAcc, xGyr, yGyr, zGyr, time){
        var stepDistanceX = null;
        var stepDistanceY = null;
        var stepDistanceZ = null;
        if(this.t == null){
            this.t = time;
        } else {
            var tt = time - this.t;
            tt = tt / N2S;
            this.t = time;

            /*  计算x方向位移
             *  计算tt时间内x方向的平均加速度（lax上一次的加速 xLAcc这一次的加速度 tt上一次到这一次的时间间隔）
             *  计算这一次x方向的速度（v0x上一次x方向的速度 vtx这一次x方向上的速度）
             *  计算tt时间内x方向的位移
             */
            var x = (this.lax + xLAcc) / 2.0;
            this.vtx = this.v0x + x * tt;
            stepDistanceX = (this.v0x + this.vtx) / 2.0 * tt; 
            // this.totalDistance += stepDistanceX;
            // console.log("totalDistance --> " + this.totalDistance);

            var y = (this.lay + yLAcc) / 2.0;
            this.vty = this.v0y + y * tt;
            stepDistanceY = (this.v0y + this.vty) / 2.0 * tt; 

            var z = (this.laz + zLAcc) / 2.0;
            this.vtz = this.v0z + z * tt;
            stepDistanceZ = (this.v0z + this.vtz) / 2.0 * tt; 

            this.lax = xLAcc;
            this.v0x = this.vtx;

            this.lay = yLAcc;
            this.v0y = this.vty;

            this.laz = zLAcc;
            this.v0z = this.vtz;
        }
        return [stepDistanceX, stepDistanceY, stepDistanceZ, this.vtx];
    }

    this.reset = function(){
        this.v0x = 0.0;
        this.vtx = 0.0;
        this.v0y = 0.0;
        this.vty = 0.0;
        this.v0z = 0.0;
        this.vtz = 0.0;
        this.t = null;
        this.lax = 0.0;
        this.lay = 0.0;
        this.laz = 0.0;
    }
}